﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Filters;
using RAP.Framework.Libary.Utils;

namespace RAP.Web.Libary.Controller.Filters
{
  /// <summary>
  /// 分页参数预处理
  /// </summary>
  public class PagingParamFilter : IAsyncActionFilter
  {
    /// <summary>
    /// 
    /// </summary>
    /// <param name="context"></param>
    /// <param name="next"></param>
    /// <returns></returns>
    public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
    {
      var controller = context.Controller as IWebController;

      var page = controller.QueryParams.ToInt32("page", 0);
      if (page > 0)
      {
        var rows = controller.QueryParams.ToInt32("rows");
        if (rows <= 0)
        {
          rows = 10;
          controller.QueryParams["rows"] = rows;
        }
        controller.QueryParams["startIndex"] = (page - 1) * rows;
      }
      await next();
    }
  }
}
